package com.blast.cn.adapter

import android.view.View
import androidx.core.content.ContextCompat
import com.blast.cn.R
import com.chad.library.adapter.base.BaseViewHolder
import com.wxl.common.adapter.AbsKeyEventAdapter
import com.wxl.common.room.entity.BlastRecordEntity

class BlastLogAdapter : AbsKeyEventAdapter<BlastRecordEntity>{

    val widths = ArrayList<Int>()

    constructor(data: List<BlastRecordEntity>?) : super(R.layout.item_blast_log_layout,data)

    fun setWidths(widths:ArrayList<Int>){
        this.widths.clear()
        this.widths.addAll(widths)
    }

    override fun convert(helper: BaseViewHolder, item: BlastRecordEntity) {
        super.convert(helper, item)
        if(this.widths.isNotEmpty()){
            setViewWidth(helper.getView(R.id.itemNumberText)
                ,helper.getView(R.id.itemBlastLogDateText)
                ,helper.getView(R.id.itemBlastLogNumText)
                ,helper.getView(R.id.itemBlastLogStatusText)
                ,helper.getView(R.id.itemBlastLogRemarkText)
                )
        }

        helper.setText(R.id.itemNumberText,(helper.adapterPosition+1).toString())
        helper.setText(R.id.itemBlastLogDateText,item.blastTime)
        helper.setText(R.id.itemBlastLogNumText,item.blastTotalNumber.toString())
        if(item.isUpload == 1) {
            helper.setText(R.id.itemBlastLogRemarkText,"有效")
            helper.setText(R.id.itemBlastLogStatusText,"已上传")
            helper.setTextColor(R.id.itemBlastLogStatusText,ContextCompat.getColor(mContext,R.color.apply_item_text_color))
        } else if(item.isUpload == 2){
            helper.setText(R.id.itemBlastLogRemarkText,"")
            helper.setText(R.id.itemBlastLogStatusText,"上传中")
            helper.setTextColor(R.id.itemBlastLogStatusText,ContextCompat.getColor(mContext,R.color.gray_48))
        } else {
            helper.setText(R.id.itemBlastLogRemarkText,"")
            helper.setText(R.id.itemBlastLogStatusText,"未上传")
            helper.setTextColor(R.id.itemBlastLogStatusText,ContextCompat.getColor(mContext,android.R.color.holo_red_dark))
        }
    }


    private fun setViewWidth(vararg vs: View){
        (0 until vs.size).forEach { i ->
            vs[i].layoutParams.width = widths[i]
        }
    }
}